package com.ruoyi.travel.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Random;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.travel.mapper.TravelOrderMapper;
import com.ruoyi.travel.domain.TravelOrder;
import com.ruoyi.travel.service.ITravelOrderService;

@Service
public class TravelOrderServiceImpl implements ITravelOrderService {
    @Autowired
    private TravelOrderMapper travelOrderMapper;
    @Autowired
    private SysUserMapper userMapper;

    /**
     * 查询订单管理
     *
     * @param orderId 订单管理主键
     * @return 订单管理
     */
    @Override
    public TravelOrder selectTravelOrderByOrderId(Long orderId) {
        return travelOrderMapper.selectTravelOrderByOrderId(orderId);
    }

    /**
     * 查询订单管理列表
     *
     * @param travelOrder 订单管理
     * @return 订单管理
     */
    @Override
    public List<TravelOrder> selectTravelOrderList(TravelOrder travelOrder) {
//        travelOrder.setUserId(SecurityUtils.getUserId());
//        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
//            travelOrder.setUserRoleName("admin");
//        } else if (SecurityUtils.hasRole("地陪")) {
//            travelOrder.setUserRoleName("地陪");
//        } else if (SecurityUtils.hasRole("定制师")) {
//            travelOrder.setUserRoleName("定制师");
//        } else if (SecurityUtils.hasRole("客服")) {
//            travelOrder.setUserRoleName("客服");
//        }
        System.out.println("sfhpadgfuiawrhaeriupghaefdghasfjgherg===========" + travelOrder);
        return travelOrderMapper.selectTravelOrderList(travelOrder);
    }

    /**
     * 新增订单管理
     *
     * @param travelOrder 订单管理
     * @return 结果
     */
    @Override
    public int insertTravelOrder(TravelOrder travelOrder) {
        // 生成 consultNumber：zx + 时间戳 (毫秒)+ 四位随机数
        StringBuilder orderNumber = new StringBuilder();
        orderNumber.append("dd"); // 固定前缀
        orderNumber.append(System.currentTimeMillis()); // 毫秒级时间戳
// 生成四位随机数（0000-9999）
        Random random = new Random();
        int randomNum = random.nextInt(10000);
        orderNumber.append(String.format("%04d", randomNum)); // 不足四位补 0
// 设置生成的编号到实体类
        travelOrder.setOrderNumber(orderNumber.toString());
        travelOrder.setCreateTime(DateUtils.getNowDate());
        travelOrder.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        travelOrder.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        return travelOrderMapper.insertTravelOrder(travelOrder);
    }

    /**
     * 修改订单管理
     *
     * @param travelOrder 订单管理
     * @return 结果
     */
    @Override
    public int updateTravelOrder(TravelOrder travelOrder) {
        travelOrder.setUpdateTime(DateUtils.getNowDate());
        return travelOrderMapper.updateTravelOrder(travelOrder);
    }

    /**
     * 批量删除订单管理
     *
     * @param orderIds 需要删除的订单管理主键
     * @return 结果
     */
    @Override
    public int deleteTravelOrderByOrderIds(Long[] orderIds) {
        return travelOrderMapper.deleteTravelOrderByOrderIds(orderIds);
    }

    /**
     * 删除订单管理信息
     *
     * @param orderId 订单管理主键
     * @return 结果
     */
    @Override
    public int deleteTravelOrderByOrderId(Long orderId) {
        return travelOrderMapper.deleteTravelOrderByOrderId(orderId);
    }

}
